﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.Messaging.Photos;
using NCP.Recruiting.Domain.Aggregates.Photoes;
using NCP.Recruiting.ApplicationServices.Contracts;
using NCP.Infrastructure.Common;
using NCP.Recruiting.ApplicationServices.ViewModels;

namespace NCP.Recruiting.ApplicationServices.Implementations
{
    public class PhotoAppService : IPhotoAppService
    {
        private readonly IPhotoRepository _photoRepository;

        public PhotoAppService(IPhotoRepository photoRepository)
        {
            this._photoRepository = photoRepository;
        }


        public CreatePhotoResponse CreatePhoto(CreatePhotoRequest request)
        {
            var response = new CreatePhotoResponse();
            try
            {
                var entity = request.PhotoProperties.CopyItem<ICreatePhotoCommand, Photo>();
                entity.GenerateNewIdentity();
                entity.Title = request.PhotoProperties.Title;
                entity.PhotoBinary = request.PhotoProperties.PhotoBinary;
                entity.MimeType = request.PhotoProperties.MimeType;
                entity.Width = request.PhotoProperties.Width;
                entity.Height = request.PhotoProperties.Height;
                entity.Size = request.PhotoProperties.Size;
                entity.SortOrder = request.PhotoProperties.SortOrder;
                entity.CompanyId = request.PhotoProperties.CompanyId;
                entity.Deleted = false;
                entity.DateCreated = DateTime.Now;
                entity.LastUpdated = DateTime.Now;

                _photoRepository.Add(entity);
                _photoRepository.UnitOfWork.Commit();


            }
            catch (Exception ex)
            {

                response.Exception = ex;
            }

            return response;
        }

        public DeletePhotoResponse DeletePhoto(DeletePhotoRequest request)
        {
            var response = new DeletePhotoResponse();
            try
            {
                var entity = _photoRepository.Get(request.Id);
                entity.Deleted = true;
                entity.LastUpdated = DateTime.Now;
                _photoRepository.Modify(entity);
                _photoRepository.UnitOfWork.Commit();
            }
            catch (Exception ex)
            {
                response.Exception = ex;
            }
            return response;
        }

        public GetPhotoResponse GetPhoto(GetPhotoRequest request)
        {
            var photo = _photoRepository.Get(request.Id);
            var viewModel = photo.CopyItem<Photo, PhotoViewModel>();
            return new GetPhotoResponse() { ViewModel = viewModel };
        }

        public GetPhotosResponse GetPhotos(GetPhotosRequest request)
        {
            var photos = _photoRepository.AllMatching(PhotoSpecifications.GetPhotoNotDel(request.Id)).Select(x => new PhotoViewModel()
            {

                Id = x.Id,
                Title = x.Title,
                PhotoBinary = x.PhotoBinary,
                MimeType = x.MimeType,
                Width = x.Width,
                Height = x.Height,
                Size = x.Size,
                SortOrder = x.SortOrder


            });
            return new GetPhotosResponse() { Photos = photos };
        }

        public FindPhotosResponse FindPhotos(FindPhotosRequest request)
        {
            throw new NotImplementedException();
        }
    }
}
